#!/bin/bash

# Verify the tigase status
 tigase_stat=`curl --write-out %{http_code} --silent --output /dev/null http://10.0.10.18:9090`
 if [ $tigase_stat != 200 ]; then
   echo "Tigase is not running well! Please check the logs: /vagrant/tigase_logs from workspace."
   exit $tigase_stat
 else
   echo "Tigase is up. Go on with the test...."
 fi

# Verify the bcs site status

 echo "========"
 echo "======== Create Daemon Users ====== "
 curl http://10.0.10.18/superAdmin/createUser/email/def-user@test-d.com/password/123456/firstName/def-user/lastName/test
 curl http://10.0.10.18/superAdmin/createUser/email/stats-user@test-d.com/password/123456/firstName/stats-user/lastName/test

# create users for tigase test-suite
 echo "========"
 echo "======== Create Selenium Users ====== "
 for i in {1..55} 
 do
    echo "----"
    echo "Create user selenium$i"
    curl http://10.0.10.18/superAdmin/createUser/email/selenium$i@performance.com/password/123456
    echo $i
 done

 echo "Make sure Test Users are up before running test suite..."
 for (( i=1; i<=6; i++ ))
 do
  if ( curl -s http://10.0.10.18:9090/test/startTestUser|grep -q success )
  then
    echo " ======== Test Users are online. Begin test suite ======== "
    cd /var/lib/jenkins/tigase-testsuite && git pull && mvn package && java -Dfile.encoding=UTF-8 -classpath jars/tigase-testsuite.jar:libs/tigase-utils.jar:libs/tigase-xmltools.jar:libs/junit.jar:libs/hamcrest-core.jar tigase.test.TestSuite -script scripts/all-xmpp-tests.xmpt -serverip 10.0.10.18 -output-format junitXml && cp www/functional-tests.html.xml $WORKSPACE/ && exit 0
  fi
  echo "----"
  echo "Wait for one more minute to make sure Test Users are online. Set the timeout to 6 minutes."
  sleep 1m
 done

echo "Test Users are not online in 6 mins!"
echo "Tigase Status output - http://10.0.10.18:9090/status"
curl http://10.0.10.18:9090/status
exit 1
